define(["angularAMD"], function (angularAMD) {
    angularAMD.service('PasswordService', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog){
            this.init = function(options){
                //20240226 modify by wangzy for M#0158214: 深圳竹素：前端用户变更密码时，可以在密码中输入标点符号，但后端修改时会报错，后端理应也允许输入
                function validatePassword(password) {
                    const regex = /[^a-zA-Z0-9]/;
                    return !regex.test(password);
                }
                function changePassword(account, password, new_password, callback) {
                    var msg = $filter('translate')('password.msg.validate_password');
                    if (!validatePassword(new_password)) {
                        $rootScope.showAlert(msg);
                        return;
                    }
                    $MMWService.sendToServer({
                        uri:'ServicesSTD.Module_SYS.user_password_change_process',
                        content:{
                            userno:account,
                            password:password,
                            new_password:new_password
                        },
                        success:function(data){
                            $rootScope.showToast($filter('translate')('password.msg.success'));
                            if(callback)
                                callback();
                        },
                        error: function (data) {
                            $rootScope.showAlert(data.Message);
                         
                        }
                    });
                }

                function get_user_permission(account, password, callback){
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_SYS.user_permissions_info_get_json',
                        content : {
                            login_employee_no:account,
                            login_employee_password:password
                        },
                        success : function(data){
                            if(data.employee_level == '0'){
                                if(callback)
                                    callback(data);
                            } else {
                                $rootScope.showAlert($filter('translate')('password.msg.employee_level_0', {account: account}));
                            }

                        }
                    });
                }

                //判斷是否需要管控我的最愛
                function check_permission_json(callback){
                    $MMWService.sendToServer({
                        uri:'ServicesSTD.Module_SYS.favourite_permissions_check_process',
                        content:{
                            need_login:'N'
                        },
                        success:function(data){
                            if(callback)
                                callback(data);
                        }
                    });
                }

                //確認是否為區域負責人的帳密
                function checkAreaLeader(account, password, callback)
                {
                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_SYS.check_area_leader_process',
                        content: {
                            account: account,
                            password: password,
                            equipmentno: options.equipmentno,
                            areano: options.areano
                        },
                        success: function (data) {
                            if (data.isAreaLeader) {
                                if (callback)
                                    callback(data);
                            } else {
                                $rootScope.showAlert($filter('translate')('password.msg.employee_level_0', { account: account }));
                            }

                        }
                    });
                }

                function openDialog(){
                    $mdDialog.dialog(
                        'module/system/program/password/password.html',
                        function(dialog){
                            return {
                                css_url:'module/system/program/password/password.css',
                                mode: options?options.mode:'change',
                                account : config.cache.account,
                                password : '',
                                new_password : '',
                                confirm_password : '',
                                beforeShown: function(){
                                    if(dialog.mode == 'confirm'){
                                        dialog.password = config.cache.password;
                                    }
                                },
                                showPassword: function(id){
                                    if(document.getElementById(id).type == 'password'){
                                        document.getElementById(id).type = 'text';
                                        dialog[id+'_show'] = true;
                                    } else {
                                        document.getElementById(id).type = 'password';
                                        dialog[id+'_show'] = false;
                                    }
                                },
                                back: function () {
                                    dialog.hide();
                                },
                                confirm: function () {
                                    var msg = '';
                                    if(dialog.mode == 'change'){
                                        if(dialog.password == dialog.new_password){
                                            msg += $filter('translate')('password.msg.same_password');
                                        } else if(dialog.password == ''){
                                            msg += $filter('translate')('password.msg.password_empty');
                                        } else if(dialog.new_password == ''){
                                            msg += $filter('translate')('password.msg.new_password_empty');
                                        } else if(dialog.confirm_password == ''){
                                            msg += $filter('translate')('password.msg.confirm_password_empty');
                                        } else if(dialog.new_password != dialog.confirm_password){
                                            msg += $filter('translate')('password.msg.nosame_new_password');
                                        }
                                    } else {
                                        if(dialog.account == ''){
                                            msg += $filter('translate')('password.msg.account_empty');
                                        } else if(dialog.password == ''){
                                            msg += $filter('translate')('password.msg.password_empty');
                                        }
                                    }
                                    if(msg == ''){
                                        if(dialog.mode == 'change'){
                                            changePassword(dialog.account,dialog.password, dialog.new_password,function(){
                                                dialog.hide();
                                            });
                                        } else if (dialog.mode == 'AreaLeader') {
                                            checkAreaLeader(dialog.account, dialog.password, function () {
                                                dialog.hide();
                                                if (options.feedback)
                                                    options.feedback();
                                            });
                                        } else {
                                            get_user_permission(dialog.account,dialog.password,function(){
                                                dialog.hide();
                                                if(options.feedback)
                                                    options.feedback();
                                            });
                                        }
                                    } else {
                                        $rootScope.showAlert(msg);
                                    }
                                }
                            };
                        }
                    );
                }

                if(options && options.mode == 'confirm'){
                    check_permission_json(function(data){
                        //var data = {privnostate:1};
                        if(data.privnostate == '1'){
                            openDialog();
                        } else {
                            if(options.feedback);
                                options.feedback();
                        }
                    });
                } else {
                    openDialog();
                }
            };
        }]);
});
